//
//  CustomSpriteWithTouch.m
//  jumpingfrog
//
//  Created by Tri Vo on 8/26/13.
//  Copyright 2013 Tri Vo. All rights reserved.
//

#import "CustomSpriteWithTouch.h"


@implementation CustomSpriteWithTouch

- (void)onEnter {
    [super onEnter];
    [[[CCDirector sharedDirector] touchDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
    NSLog(@"\n\nSprite onEnter");
}

- (void)onExit {
    [super onExit];
    [[[CCDirector sharedDirector] touchDispatcher] removeDelegate:self];
    NSLog(@"\n\nSprite onExit");
}

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
    BOOL isTouch = [self touched:touch];
    NSLog(@"\n\nDA TOUCH ROI NHA");
    if (isTouch) {
        [self stopAllActions];
        id enlarge = [CCScaleTo actionWithDuration:0.5f scale:1.1f];
        id resize = [CCScaleTo actionWithDuration:0.5f scale:1];
        [self runAction:[CCSequence actions:enlarge, resize, nil]];
        
    }
    return isTouch;
}

- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {
    NSLog(@"\n\nTouch end");
}

- (BOOL) touched: (UITouch *) touch {
    CGPoint touchPoint = [touch locationInView:[touch view]];
    touchPoint = [[CCDirector sharedDirector] convertToGL:touchPoint];
    CGRect rect = [self boundingBox];
    if (CGRectContainsPoint(rect, touchPoint)) {
        return YES;
    }
    return NO;
}

@end
